<?php
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$return=array();
$return['status']=model('jy_weishop/order/refund')->status;
//$return['paytype']=get_pay_types();
if($_RQ['op']=='list'){
	$pindex = max(1, intval($_RQ['page']));
	$psize = 15;
	$params=array('uniacid'=>$_SESSION['uniacid'],'plugin_name'=>'jy_mfuwu');
    $orderby='id desc';
	$total='';
	if(!empty($_RQ['ordersn'])){
		$params['ordersn']=trim($_RQ['ordersn']);
		$return['params']['ordersn']=$_RQ['ordersn'];
	}
	if(isset($_RQ['status']) && $_RQ['status']!='all'){
		$params['status']=intval($_RQ['status']);
		$return['params']['status']=$_RQ['status'];
	}
	//会员ID
	if(!empty($_RQ['mid'])){
		$params['mid']=intval($_RQ['mid']);
		$return['params']['mid']=$_RQ['mid'];
	}
	//创建时间
	if($_RQ['createtime']){
		$time_range=cfc('tools')->base64_decode($_RQ['createtime'],$_SESSION['base_key_str']);
		$time_range=json_decode($time_range,true);
		$return['params']['createtime']=$time_range;
		$params['createtime between']=array($time_range['tstart'],$time_range['tend']);
	}
	//来自别的订单
	if(!empty($_RQ['plugin_name'])){
		$params['plugin_name']=trim($_RQ['plugin_name']);
		$return['params']['plugin_name']=$_RQ['plugin_name'];
	}
	$return['list']=pdo_getslice('jy_weishop_order_refund',$params,array($pindex,$psize),$total,$fields,'',$orderby);
	foreach($return['list'] as &$item){
		$item['member']=cfc('core')->member_show($item['mid']);
		$item['paytype']=get_pay_types()[$item['paytype']];	
	}
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'export'=>'export',
		'tabs'=>array(
			array('action'=>'jy_weishop/order.refund/list/status=all&plugin_name='.$_RQ['plugin_name'],'title'=>'全部'),
			array('action'=>'jy_weishop/order.refund/list/status=0&plugin_name='.$_RQ['plugin_name'],'title'=>'待审核'),
			array('action'=>'jy_weishop/order.refund/list/status=1&plugin_name='.$_RQ['plugin_name'],'title'=>'审核通过'),
			array('action'=>'jy_weishop/order.refund/list/status=2&plugin_name='.$_RQ['plugin_name'],'title'=>'已发货'),
			array('action'=>'jy_weishop/order.refund/list/status=3&plugin_name='.$_RQ['plugin_name'],'title'=>'已收货'),
			array('action'=>'jy_weishop/order.refund/list/status=4&plugin_name='.$_RQ['plugin_name'],'title'=>'已退款'),
			array('action'=>'jy_weishop/order.refund/list/status=5&plugin_name='.$_RQ['plugin_name'],'title'=>'被拒绝'),
		),
		'list'=>array('data'=>'list','tip'=>'[温馨提示]：</br>【<red>提示:NO_AUTH产品权限验证失败？</red>】</br>1、站点设置--支付参数--微信支付那边要上传微信支付证书；2、微信商户那边要开启“企业付款到零钱”的权限；3、属于商户的订单，只能由商户审核退款。','edit'=>false,'list'=>array(
			array('name'=>'ordersn','title'=>'订单号','type'=>'string'),
			array('name'=>'member','title'=>'会员信息','type'=>'member'),
			array('name'=>'price','title'=>'退款金额','type'=>'string'),
			array('name'=>'paytype','title'=>'支付方式','type'=>'string'),
			array('name'=>'type','title'=>'申请类型','type'=>'radio','options'=>'refund_types'),
			array('name'=>'status','title'=>'退款状态','type'=>'radio','options'=>'status'),
			array('name'=>'createtime','title'=>'申请时间','type'=>'time'),
			array('name'=>'checktime','title'=>'审核时间','type'=>'time'),
			array('name'=>'paytime','title'=>'退款完成时间','type'=>'time'),	
		),
		'action'=>array(	
			array('type'=>'modal','op'=>'look','title'=>'退款详情'),
			array('type'=>'modal','op'=>'check','title'=>'审核','show'=>array('status'=>0)),
			array('type'=>'post','op'=>'receive','title'=>'确认收货','show'=>array('status'=>2)),
			array('op'=>'pay','title'=>'确认退款','type'=>'modal','show'=>array('status'=>1,'type'=>array(0,3,4))),
		),),
		'search'=>array(
			array('name'=>'ordersn','width'=>15,'title'=>'订单号','type'=>'string'),
			array('name'=>'status','width'=>15,'title'=>'状态','type'=>'radio','options'=>'status'),
			array('type'=>'choose','width'=>15,'title'=>'会员id','name'=>'mid','action'=>'core/member.get_member/','choose_title'=>'选择会员'),
		)
	);
	$return['refund_types']=$this->refund_types;
	$return['htmls']=$htmls;	
	exi($return);
}
if($_RQ['op']=='export'){
	if(!empty($_RQ['ordersn'])){
		$params['ordersn']=trim($_RQ['ordersn']);
	}
	if(!empty($_RQ['status'])){
		$params['status']=intval($_RQ['status']);
	}
	//会员ID
	if(!empty($_RQ['mid'])){
		$params['mid']=intval($_RQ['mid']);
		$return['params']['mid']=$_RQ['mid'];
	}
	//创建时间
	if($_RQ['createtime']){
		$time_range=cfc('tools')->base64_decode($_RQ['createtime'],$_SESSION['base_key_str']);
		$time_range=json_decode($time_range,true);
		$return['params']['createtime']=$time_range;
		$params['createtime between']=array($time_range['tstart'],$time_range['tend']);
	}
	//来自别的订单
	if(!empty($_RQ['plugin_name'])){
		$params['plugin_name']=trim($_RQ['plugin_name']);
		$return['params']['plugin_name']=$_RQ['plugin_name'];
	}
	$params['uniacid']=$_SESSION['uniacid'];
	$order=pdo_getall('jy_weishop_order_refund',$params);
	$status=array('1'=>'待发货','2'=>'待收货','3'=>'已完成','4'=>'已关闭');
	$paytype=array('1'=>'微信支付','2'=>'余额支付');
	$ispay=array('0'=>'未支付','1'=>'已支付');
	foreach($order as &$v){
		$v['nickname']=pdo_getcolumn('core_members',array('id'=>$v['mid']),'nickname');		
		$address=pdo_get('core_address',array('id'=>$v['addressid'],'uniacid'=>$_SESSION['uniacid']));
		$v['address']=model('core/member')->get_address_str($v['addressid']);
		$v['mobile']=$address['mobile'];
		$v['realname']=$address['realname'];
		$v['status']=$status[$v['status']];
		$v['paytype']=$paytype[$v['paytype']];
		$v['ispay']=$ispay[$v['ispay']];
		if(!$v['nickname']){
			$v['nickname']='匿名用户';
		}
		$v['createtime']=date('Y-m-d H:i:s',$v['createtime']);
	}
	$header=array(
		'ordersn'=>'订单号',
		'nickname'=>'买家昵称',
		'price'=>'退款金额',
		'createtime'=>'申请时间',
	);
	cfc('tools')->export2excel($header,$order,'退款记录');
}
if($_RQ['op']=='check'){
	if($_RQ['postdata']){
		$post=get_postdata();
		if(!$post['status'] && !$post['status']){
			exi('请设置是否审核以及审核理由！','error');
		}
		if($post['status']==1 && $post['orderstatus']==1 && $post['orderispay']==1){
			model('jy_weishop/order/refund')->refund_check($post['id'],3,$post['reason']);
		}elseif($post['status']==1 && $post['orderstatus']==1 && $post['orderispay']==0){
			exi('货到付款订单，无法申请！','error');
		}else{
			$status=$post['status']==2?5:$post['status'];
			model('jy_weishop/order/refund')->refund_check($post['id'],$status,$post['reason']);
		}
		exi('审核成功！','','/order.refund/list/');
	}
	$id=intval($_RQ['id']);
	$item=pdo_get('jy_weishop_order_refund',array('id'=>$id),array('id','images','content','orderid','paytype'));
	$order=pdo_get('jy_weishop_order',array('uniacid'=>$_SESSION['uniacid'],'id'=>$item['orderid']));
	if($order['merchid']){
		exi('该订单属于商户的！您不能处理！','error');
	}
	$item['orderstatus']=$order['status'];
	$item['orderstatustitle']=$this->order_status[$item['orderstatus']];
	$item['orderispay']=$order['ispay'];
	$item['images']=$item['images']?json_decode($item['images'],true):array();
	$item['paytype']=get_pay_types()[$item['paytype']];	
	$return['item']=$item;
	$htmls=array(
		'type'=>'edit',
		'desc'=>'退款申请审核',
		'edit'=>array(
			array('type'=>'string','title'=>'订单状态','name'=>'orderstatustitle','read'=>true),
			array('type'=>'copy','name'=>'paytype','title'=>'退款类型'),
			array('type'=>'radio','title'=>'审核','name'=>'status','options'=>'status'),
			array('type'=>'text','title'=>'审核理由','name'=>'reason'),
			array('type'=>'images','title'=>'卖家上传的图片','name'=>'images','read'=>true),
			array('type'=>'text','title'=>'卖家退款说明','name'=>'content','read'=>true),
			
		)
	);
	$return['status']=array(
		'1'=>'审核通过',
		'2'=>'审核不通过',
	);
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='receive'){
	$id=intval($_RQ['id']);
	$item=pdo_get('jy_weishop_order_refund',array('id'=>$id),array('id','images','content','orderid','paytype'));
	$order=pdo_get('jy_weishop_order',array('uniacid'=>$_SESSION['uniacid'],'id'=>$item['orderid']));
	if($order['merchid']){
		exi('该订单属于商户的！您不能处理！','error');
	}
	model('jy_weishop/order/refund')->refund_receive($id);
	exi('收货成功！','','/order.refund/list/');
}
if($_RQ['op']=='pay'){
	if($_RQ['postdata']){
		$post=get_postdata();
		$id=intval($post['id']);
		model('jy_weishop/order/refund')->refund_pay($id);
		exi('打款成功！','','/order.refund/list/');
	}
	$id=intval($_RQ['id']);
	$item=pdo_get('jy_weishop_order_refund',array('id'=>$id));
	if(!$item){
		exi('指定退款申请不存在~','error');
	}	
	$member=model('core/core')->member_show($item['mid']);
	$item['wxgid']=pdo_getcolumn('core_fans',array('mid'=>$item['mid'],'uniacid'=>$_SESSION['uniacid']),'wxgid');
	if($member['from_os']==1){
		$item['apptitle']=pdo_getcolumn('core_account_wxapp',array('id'=>$item['wxgid']),'title');
	}else{
		$item['apptitle']=pdo_getcolumn('core_account_wechat',array('id'=>$item['wxgid']),'title');
	}
	$item['paytype']=get_pay_types()[$item['paytype']];	
	$return['item']=$item;
	$return['htmls']=array(
		'type'=>'edit',
		'edit'=>array(
			array('type'=>'tip','title'=>'【温馨提示】：如果打款会员是<red>小程序</red>会员，会显示所属《小程序》，否则可能会打款不成功!默认打款使用的是公众号！'),
			array('name'=>'apptitle','title'=>'所属','type'=>'copy'),
			array('type'=>'copy','name'=>'paytype','title'=>'退款类型'),
			array('type'=>'copy','name'=>'price','title'=>'退款金额'),
			array('type'=>'copy','name'=>'price','title'=>'退款到账金额'),
			array('type'=>'copy','name'=>'realname','title'=>'姓名','show'=>array('type'=>array(1,2))),
			array('type'=>'copy','name'=>'alipay','title'=>'支付宝账号','show'=>array('type'=>array(1))),
			array('type'=>'copy','name'=>'bankname','title'=>'所属银行','show'=>array('type'=>array(2))),
			array('type'=>'copy','name'=>'bankcard','title'=>'银行卡号','show'=>array('type'=>array(2))),
		),
	);
	exi($return);
}
if($_RQ['op']=='look'){
	$id=intval($_RQ['id']);
	$item=pdo_get('jy_weishop_order_refund',array('id'=>$id));
	if(!$item){
		exi('指定退款申请不存在~','error');
	}
	$item['paytype']=get_pay_types()[$item['paytype']];	
	$return['item']=$item;
	$return['htmls']=array(
		'type'=>'look',
		'look'=>array(
			array('type'=>'copy','name'=>'paytype','title'=>'退款类型'),
			array('type'=>'copy','name'=>'price','title'=>'退款金额'),
			array('type'=>'copy','name'=>'price','title'=>'退款到账金额'),
			array('type'=>'copy','name'=>'realname','title'=>'姓名','show'=>array('type'=>array(1,2))),
			array('type'=>'copy','name'=>'alipay','title'=>'支付宝账号','show'=>array('type'=>array(1))),
			array('type'=>'copy','name'=>'bankname','title'=>'所属银行','show'=>array('type'=>array(2))),
			array('type'=>'copy','name'=>'bankcard','title'=>'银行卡号','show'=>array('type'=>array(2))),
		),
	);
	exi($return);
}
if($_RQ['op']=='del'){

	$id=intval($_RQ['id']);
	if($id){
		pdo_delete('jy_weishop_order_refund',array('id'=>$id));	
	}
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
		foreach($del as $id){
			$id=intval($id);
			if($id){
				pdo_delete('jy_weishop_order_refund',array('id'=>$id));	
			}
		}
	}
	exi('删除成功！','','/order.refund/list/');
}

?>